package com.alibaba.datax.plugin.writer.xmlfilewriter;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.FileWriterWithEncoding;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


public class Dom4jXmlUtil {

    public static void main(String[] args) {
        test04();
//        long start = System.currentTimeMillis();
//        Document document = generateXmlStructure();
//        Element rowElement = addRowElement(document, "blxw");
//        for (int i = 0; i < 10; i++) {
//            addField(rowElement, "SESSONID", "varchar", "1501051042330008");
//        }
//        createXml("d:/test/test", document);
//        System.out.println("运行时间：" + (System.currentTimeMillis() - start));
    }

    public static void test04() {
// 得到当前日期时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 将日期时间格式化为字符串
        String format = now.format(formatter);
        System.out.println("format = " + format);
// 将字符串解析为日期时间
        LocalDate parse = LocalDate.parse("1985-09-23", formatter);
        System.out.println("parse = " + parse);
    }

    // 4、生成子节点及子节点内容

    /**
     * 生成一行数据
     *
     * @param document
     * @param tableName
     */
    public static Element addRowElement(Document document, String tableName) {
        Element root = document.getRootElement();
        Element rowElement = root.addElement("data");
        rowElement.addAttribute("tableName", tableName);
        return rowElement;
    }

    /**
     * 生成一个字段的数据
     *
     * @param rowElement
     * @param key
     * @param type
     * @param value
     */
    public static void addField(Element rowElement, String key, String type, String value) {
        Element field = rowElement.addElement("field");
        field.addAttribute("key", key);
        field.addAttribute("type", type);
        field.addAttribute("value", value);
    }

    public static Document generateXmlStructure() {
        // 1、创建document对象
        Document document = DocumentHelper.createDocument();
        // 2、创建数据交换根节点exchange
        Element exchange = document.addElement("exchange");
        // 3、向rss节点添加version属性
        exchange.addAttribute("title", "数据交换平台");
        return document;
    }

    /**
     * 生成xml方法
     */
    public static void createXml(String filePath, Document document) {
        try {
            // 5、设置生成xml的格式
            OutputFormat format = OutputFormat.createPrettyPrint();
            // 设置编码格式
            format.setEncoding("UTF-8");

            // 6、生成xml文件
            File file = new File(filePath + ".xml");
            XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
            // 设置是否转义，默认使用转义字符
            writer.setEscapeText(false);
            writer.write(document);
            writer.close();
            System.out.println("生成rss.xml成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("生成rss.xml失败");
        }
    }

    private static void test1() throws IOException {
        Writer test = new FileWriterWithEncoding("C:/Users/Administrator/Desktop/test/test.txt", StandardCharsets.UTF_8, true);
        test.write(String.format("%s%s", StringUtils.join("111111123"), IOUtils.LINE_SEPARATOR, IOUtils.LINE_SEPARATOR));
        test.flush();
        System.out.println(test.toString());
    }
}
